<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <script>
    // 1.移除数组 [1, 2, 3, 4, 2] 中的2。不要直接修改数组，结果返回新的数组
    var arr = [1, 2, 3, 4, 2]
    var newArr = [];
    for (var i = 0; i < arr.length; i++) {
      if (arr[i] != 2) {
        newArr.push(arr[i]);
      }
    }
    console.log(newArr)

    var arr = [1, 2, 3, 4, 2];
    var newArr = arr.filter(item => item !== 2);
    console.log(newArr); // [1, 3, 4]

    // 2.定义一个函数,能在数组 [1, 2, 3, 4, 2] 的 "2"后面添加元素 "JavaScript"。不要直接修改数组 arr，结果返回新的数组
    var arr = [1, 2, 3, 4, 2];
    function add(arr, name) {
      var newArr = arr.filter(item => item !== 2);
      var newArr2 = arr.filter(item => item === 2);
      for (let i = 0; i < newArr2.length; i++) {
        newArr.push(newArr2[i], name);
      }
    }
    console.log(add(arr, "javasceipt"))


    // 3.统计数组 [1, 2, 3, 4, 2] 中2出现的次数
    var arr = [1, 2, 3, 4, 2]
    var newArr = [];
    for (var i = 0; i < arr.length; i++) {
      if (arr[i] == 2) {
        newArr.push(arr[i]);
      }
    }
    console.log(newArr.length)

    // 4.找出数组 [1, 2, 3, "JavaScript", 4, "JavaScript", 2, 4, "JavaScript"] 中重复出现过的元素,并用数组将重复元素装起来输出
    let arr = [1, 2, 3, "JavaScript", 4, "JavaScript", 2, 4, "JavaScript"];

    let uniqueValues = [...new Set(arr)];

    console.log(uniqueValues);


    // 5.在数组里面输出年龄小于17的对象
    let arr5 = [
      {
        name: '111',
        sex: 'boy',
        age: 18,
      },
      {
        name: '222',
        sex: 'girl',
        age: 17,
      },
      {
        name: '333',
        sex: 'boy',
        age: 16,
      },
      {
        name: '444',
        sex: 'girl',
        age: 15,
      },
      {
        name: '555',
        sex: 'boy',
        age: 20,
      },
    ];

    let under17 = arr5.filter(obj => obj.age < 17);

    console.log(under17);

    // 6. 数组扁平化, 把数组[1, [2, [3, 4, 5]]]转成[1, 2, 3, 4，5] (多维数组 --> 一维数组)
    console.log(arr.flat(Infinity));




    function fn(arr) {
      let newArr = []
      for (let i = 0; i < arr.length; i++) {
        if (!array.isArray(arr[i])) {
          newArr.push(arr[i]);

        } else {
          newArr = newArr6.concat(arr[i]);
        }
        return newArr
      }
    }


    // 7. 使用reduce实现[1,2,3,4,9]数组中所有成员的和
    let array = [1, 2, 3, 4, 9];

    let sum = array.reduce((total, currentValue) => {
      return total + currentValue;
    }, 0);

    console.log(sum);

    // 8. 数组去重的方法 (4种)
    //1.


  </script>
</body>

</html>